package com.jltx.xq.videoeditorlearnpro;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;

import com.jltx.xq.videoeditorlearnpro.permissions.dispatcher.CommonPermissionsDispatcher;
import com.jltx.xq.videoeditorlearnpro.permissions.dispatcher.PermissionRequest;
import com.jltx.xq.videoeditorlearnpro.ui.RecordedActivity;
import com.jltx.xq.videoeditorlearnpro.permissions.dispatcher.XqRuntimePermissionsUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseAppCompatActivity {
    private final int CARMER_PERMISSION_REQUEST_CODE = 0xFF01;
    private final int LINK_PERMISSION_REQUEST_CODE = 0xFF10;
    /**
     * 打开拍摄需要相机权限，录音权限，sd卡读写权限
     */
    private final String[] CARMER_PERMISSIONS = new String[]{ Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO ,Manifest.permission.WRITE_EXTERNAL_STORAGE};


    @BindView(R.id.btn1)
    protected Button btn1;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(mCarmerPermissionsDispatcher != null){
            mCarmerPermissionsDispatcher.onRequestPermissionsResult(this,requestCode,grantResults);
        }
    }

    @OnClick({R.id.btn1})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn1:
                mCarmerPermissionsDispatcher.showPermissionForOperateWithCheck(MainActivity.this);
                break;
            default:
                break;
        }
    }


    /**
     * 打开拍摄权限控制类
     */
    private CommonPermissionsDispatcher mCarmerPermissionsDispatcher = new CommonPermissionsDispatcher(CARMER_PERMISSION_REQUEST_CODE,CARMER_PERMISSIONS) {
        @Override
        public void allowForOperater(boolean isFromRequestPermission) {
            openCarmer();
        }

        @Override
        public void rationaleForOperater(final PermissionRequest request) {
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle("权限申请")
                    .setMessage("拍照需要访问相机权限、录音权限、读写存储权限才可以正常使用哦")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            request.proceed();
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            request.cancel();
                        }
                    })
                    .create()
                    .show();
        }

        @Override
        public void deniedForOperater() {
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle("权限申请")
                    .setMessage("拍照需要访问相机权限、录音权限、读写存储权限才可以正常使用哦！")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    })
                    .create()
                    .show();
        }

        @Override
        public void neverAskAgainForOperater() {
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle("权限申请")
                    .setMessage("拍照需要访问相机权限、录音权限、读写存储权限,不开启将无法正常使用！")
                    .setPositiveButton("去设置", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //调用系统设置页面
                            XqRuntimePermissionsUtils.startInstalledAppDetailsActivity(MainActivity.this);
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    })
                    .create()
                    .show();
        }
    };


    private void openCarmer(){
        Intent intent = new Intent(this, RecordedActivity.class);
        this.startActivity(intent);
    }
}
